package com.infindo.iflower.util;

import java.io.IOException;
import java.io.InputStream;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class AssetsUtil {
	public static final String PICTURE_PATH = "pictures";
	public static final String PICTURE_PATH_BIG = "bigpictures";
    public static Drawable getPictureDrawable(Context context, String name) {
    	
    	try {
    		if("/sdcard".startsWith(name)){
    			System.out.println("..true");
    			return new BitmapDrawable(context.getAssets().open(name));
    		}
			return new BitmapDrawable(context.getAssets().open(PICTURE_PATH + "/" + name));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    public static Drawable getBigPictureDrawable(Context context, String name) {
    	
    	try {
    		if("/sdcard".startsWith(name)){
    			System.out.println("..true");
    			return new BitmapDrawable(context.getAssets().open(name));
    		}
			return new BitmapDrawable(context.getAssets().open(PICTURE_PATH_BIG + "/" + name));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    public static InputStream getPictureStream(Context context, String assetsPath, String name) {
    	
    	try {
			return context.getAssets().open(assetsPath + name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
/*    public static InputStream getBigPictureStream(Context context, String name) {
    	
    	try {
			return context.getAssets().open(PICTURE_PATH_BIG + "/" + name);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    */
    public static String[] getPictureList(Context context) {
    	
    	try {
			return context.getAssets().list(PICTURE_PATH);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    public static String[] getBigPictureList(Context context) {
    	
    	try {
			return context.getAssets().list(PICTURE_PATH_BIG);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
    
    public static boolean saveAssetsToSdcard(Context context, String assetsPath, String assetsName, String filePath) {
    	try {
    		if (FileUtil.existFile(filePath) == null) {
    			FileUtil.createDir(filePath);
    		}
    			
	    	InputStream is = AssetsUtil.getPictureStream(context, assetsPath, assetsName);
	    	return FileUtil.writeFile(filePath + assetsName, is);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return false;
    }
    
    public static boolean saveAssetsToLocal(Context context, String assetsPath, String assetsName, String filePath) {
    	try {
    		if (FileUtil.existFile(filePath) == null) {
    			FileUtil.createDir(filePath);
    		}
    			
	    	InputStream is = AssetsUtil.getPictureStream(context, assetsPath, assetsName);
	    	return FileUtil.writeFile(filePath + assetsName, is);
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    	return false;
    }
}
